package map_;

import java.util.*;
import java.util.Map.Entry;

public class MapFor {
    public static void main(String[] args) {
//        赋初值
        Map hashMap=new HashMap();
        hashMap.put("1","a");
        hashMap.put("2","b");
        hashMap.put("3","c");
        hashMap.put("4","d");
        hashMap.put("5","e");
//        通过EntrySet遍历
        System.out.println("---------通过EntrySet遍历----------");
        Set entrySet = hashMap.entrySet();
        System.out.println(entrySet.getClass());
        for (Object entry:entrySet) {
            Map.Entry m= (Entry) entry;
            System.out.println(m.getKey()+" - "+m.getValue());
        }
//        通过迭代器遍历
        System.out.println("---------通过EntrySet迭代器遍历----------");
        Iterator iterator2 = entrySet.iterator();
        while (iterator2.hasNext()) {
            Map.Entry next =  (Map.Entry) iterator2.next();
            System.out.println(next.getKey()+" - "+next.getValue() );
        }

//        通过KeySet遍历
        System.out.println("---------通过KeySet遍历----------");

        Set keySet = hashMap.keySet();
        for (Object key:keySet) {
            System.out.println(key+" - "+hashMap.get(key));
        }
//        通过KeySet迭代器遍历
        System.out.println("---------通过KeySet迭代器遍历----------");
        Iterator iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Object key =  iterator.next();
            System.out.println(key+" - "+hashMap.get(key));
        }
//        通过values遍历
        System.out.println("---------通过values遍历----------");

        Collection values = hashMap.values();
        for (Object value:values) {
            System.out.println(value);
        }
//        通过迭代器遍历
        System.out.println("---------通过values迭代器遍历----------");
        Iterator iterator1 = values.iterator();
        while (iterator1.hasNext()) {
            Object value =  iterator1.next();
            System.out.println(value);
        }
//
        System.out.println("---------Lambda遍历-----------");
        hashMap.forEach((k,v)-> System.out.println(k+" - "+v));
    }
}
